#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),
      m_comm_setting_dg(new Dialog_Comm_setting(this)),
      m_serial_connect(new Serial_connect(this))
{
    ui->setupUi(this);
    //QT5 signal and SLOt recommanded
//    QObject::connect(&button, &QPushButton::clicked,&app, &QApplication::quit);
    QObject::connect(m_comm_setting_dg,&Dialog_Comm_setting::send_port_settings,m_serial_connect,&Serial_connect::set_port_settings);
    QObject::connect(m_serial_connect,&Serial_connect::sendError,this,&MainWindow::dealError);
    QObject::connect(m_serial_connect,&Serial_connect::startInfo,m_serial_connect,&Serial_connect::Deal_signal);

}


MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actionPort_setting_triggered()
{
    m_comm_setting_dg->show();
}

void MainWindow::on_actionController_setting_triggered()
{


}


void MainWindow::on_pushButton_connect_clicked()
{
    if(ui->pushButton_connect->text()=="Connect")
    {
        if(m_serial_connect->MB_connect())
        {
            ui->pushButton_connect->text()="Disconnect";
            return;
        }
        else
        {
            QMessageBox::information(NULL, "Warning","Connect seiral port fail, please check connecting!!");
        }

    }
    else
    {
        m_serial_connect->MB_disconnect();
        ui->pushButton_connect->text()="Connect";

    }

}


void MainWindow::on_pushButton_initial_clicked()
{

    if(ui->pushButton_connect->text()=="Disconnect")
    {
        if((ui->radioButton_St->isChecked()==true)||(ui->radioButton_Bt->isChecked()==true))
        {
            bool is_smallT;
            if(ui->radioButton_St->isChecked())
            {
                is_smallT=true;
                m_serial_connect->setStage_count(2);
                m_serial_connect->setIs_smallTable(true);
                m_serial_connect->setCurrent_state(Modbus_flag::Init);
            }
            else
            {
                is_smallT=false;
            }
            QVector<QString> Com_data;
            qDebug()<<"send init info";
            emit m_serial_connect->startInfo(Modbus_flag::Init,Com_data,is_smallT);

        }
        else
        {
            QMessageBox::information(NULL, "Warning","Please choose one turntable");
        }

    }
    else
    {
        QMessageBox::information(NULL, "Warning","Please connect the port first!");
    }

}

void MainWindow::dealError(QString errorinfo)
{
    QMessageBox::information(NULL, "Error",errorinfo);
}

